package jee.boot.module.service;

import jee.boot.common.utils.SpringContextHolder;
import jee.boot.module.websocket.WebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 发送socket消息
 */
@Service
public class WebSocketService {
    @Autowired
    private WebSocketHandler webSocketHandler;

    /**
     * 定点通知(通知到人)
     *
     * @param sid 用户id
     * @param msg 消息对象
     */
    public void sendMessage(String sid, Object msg) {
        if (this.webSocketHandler == null) {
            this.webSocketHandler = (WebSocketHandler)SpringContextHolder.getApplicationContext().getBean(WebSocketHandler.class);
        }
        webSocketHandler.sendMessage(msg, sid);
    }

    /**
     * 广播通知,通知所有人
     *
     * @param msg 消息对象
     */
    public void sendMessage(Object msg) {
        sendMessage(null, msg);
    }
}
